package com.wzy.controller;

import com.github.pagehelper.PageInfo;
import com.wzy.entity.Kt;
import com.wzy.service.KtSevice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;

@Controller
@RequestMapping("kt")
public class KtController {
    private static final Logger log = LoggerFactory.getLogger(KtController.class);
    private KtSevice ktSevice;
    @Autowired
    public KtController(KtSevice ktSevice) {
        this.ktSevice = ktSevice;
    }

    @RequestMapping("lists")
    public String lists(Model model, @RequestParam(value = "pageIndex",defaultValue = "1") int pageIndex,
                        @RequestParam(value = "pageSize",defaultValue = "5",required = false) int pageSize){
        log.debug("查询所有课题信息");
        PageInfo page = ktSevice.lists(pageIndex,pageSize);
        model.addAttribute("cklist","lists?pageIndex=");//给页数默认值
        model.addAttribute("page",page);
        //获得当前页
        return "cklist";
    }
    @RequestMapping("mlists")
    public String mlists(Model model, @RequestParam(value = "pageIndex",defaultValue = "1") int pageIndex,
                        @RequestParam(value = "pageSize",defaultValue = "2",required = false) int pageSize){
        log.debug("查询所有课题信息");
        PageInfo page = ktSevice.lists(pageIndex,pageSize);
        model.addAttribute("klist","lists?pageIndex=");//给页数默认值
        model.addAttribute("page",page);
        //获得当前页
        return "klist";
    }
    @RequestMapping("save")
    public String save(Kt kt) throws IOException {
        log.debug("名称:{}, 方向:{}, 教师:{} ,日期:{}", kt.getKname(), kt.getKtype(), kt.getKteacher(),kt.getKtime());
        ktSevice.save(kt);
        return "redirect:/kt/lists";//保存成功跳转到列表页面
    }

    @RequestMapping("detail")
    public String detail(Integer kid, Model model) {
        log.debug("当前查询课题id: {}", kid);
        //1.根据id查询一个
        Kt kt = ktSevice.findById(kid);
        model.addAttribute("kt", kt);
        return "updateKt";//跳转到更新页面
    }


    @RequestMapping("update")
    public String update(Kt kt) throws IOException {

        log.debug("更新之后课题信息: id:{},名称:{}, 方向:{}, 教师:{} ,日期:{}", kt.getKid(),kt.getKname(), kt.getKtype(), kt.getKteacher(),kt.getKtime());
        ktSevice.update(kt);
        return "redirect:/kt/lists";//更新成功,跳转到研究生列表
    }

    @RequestMapping("delete")
    public String delete(Integer kid){
        log.debug("删除的课题id: {}",kid);
        //1.删除数据
        ktSevice.delete(kid);
        return "redirect:/kt/lists";//跳转到员工列表
    }
}
